package com.onesports.intelligent.k12.polarlight.service.student;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.common.domain.entity.BaseEntity;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.InsertCoachDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.UpdateCoachDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.wx.UserNameAvaDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.WxUser;
import com.onesports.intelligent.k12.polarlight.domain.entity.wx.OrganizationCampusApply;
import com.onesports.intelligent.k12.polarlight.domain.vo.wx.OrganizationCampusApplyVO;
import com.onesports.intelligent.k12.polarlight.mapper.sys.WxUserMapper;
import com.onesports.intelligent.k12.polarlight.mapper.wx.OrganizationCampusApplyMapper;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.wx.WxUserPurseService;
import com.onesports.intelligent.k12.polarlight.util.BeanCopyUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @author FPH
 * @since 2022年7月13日10:40:57
 */
@Service
@RequiredArgsConstructor
public class WxUserService extends ServiceImpl<WxUserMapper, WxUser> {
    private final WxUserMapper mapper;
    private final OrganizationCampusApplyMapper organizationCampusApplyMapper;

    private final WxUserPurseService wxUserPurseService;


    /**
     * 根据教练信息手机号 获取WxUser实体，如果不存在这个人则创建完在获取id
     *
     * @param dto 入参
     * @return WxUser
     */
    public WxUser getWxUserByCoach(InsertCoachDTO dto) {
        WxUser wxUser = this.mapper.selectOne(new LambdaQueryWrapper<WxUser>()
                .eq(BaseEntity::getDeleted, false).eq(WxUser::getPhone, dto.getPhone()));
        if (ParamUtils.isNotEmpty(wxUser)) {
            LambdaUpdateWrapper<WxUser> lam = new LambdaUpdateWrapper<>();
            lam.eq(WxUser::getId, wxUser.getId()).set(WxUser::getIsCoach, true);
            this.mapper.update(null, lam);
            return wxUser;
        } else {
            WxUser entity = new WxUser();
            entity.setName(dto.getName());
            entity.setPhone(dto.getPhone());
            entity.setIsCoach(true);
            this.mapper.insert(entity);
            //添加钱包
            wxUserPurseService.addUserPurseByWxUserId(entity.getId());
            return entity;
        }

    }

    /**
     * 修改教练信息增加到WxUser
     *
     * @param dto 入参
     */
    public void updCoach(UpdateCoachDTO dto) {
        WxUser entity = new WxUser();
        entity.setName(dto.getName());
        entity.setPhone(dto.getPhone());
        entity.setId(dto.getWxUserId());
        this.mapper.updateById(entity);
    }


    /**
     * 获取我的社区申报
     *
     * @return OrganizationCampusApplyVO 小程序社区申报VO
     */
    public List<OrganizationCampusApplyVO> getApply() {
        String accountId = SecurityUtils.getAccountId();
        List<OrganizationCampusApply> entities = this.organizationCampusApplyMapper
                .selectList(new LambdaQueryWrapper<OrganizationCampusApply>()
                        .eq(OrganizationCampusApply::getDeleted, false)
                        .eq(OrganizationCampusApply::getWxUserId, accountId));
        return BeanCopyUtil.copyListProperties(entities, OrganizationCampusApplyVO::new);
    }
    /**
     * function： 根据手机号或openId获取 WxUser实体
     *
     * @param phone 手机号
     * @param openId 微信授权id
     * @return WxUser 实体类列表
     * @author YJJ
     * @since 2022/7/19 10:48
     */
    public List<WxUser> getByPhoneOrOpenId(String phone, String openId) {
        return mapper.selectList(new QueryWrapper<WxUser>().lambda()
                .eq(WxUser::getPhone, phone).or().eq(WxUser::getOpenId, openId));
    }

    /**
     * 修改当前用户的用户昵称
     * @return
     */
    public Boolean updateUserNickName(UserNameAvaDTO dto){
        WxUser wx = Optional.ofNullable(getById(SecurityUtils.getAccountId())).orElseThrow(() -> new BusinessException("用户信息不存在"));
        if(StringUtils.isNotEmpty(dto.getNickname())){
            wx.setNickname(dto.getNickname());
        }
        if(StringUtils.isNotEmpty(dto.getAvatar())){
            wx.setAvatar(dto.getAvatar());
        }
        return updateById(wx);
    }
}
